package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.player.VersionAndHardwarePlayerManager;
import com.loongtech.bi.manager.player.VersionAndHardwarePlayerManager.UserLoginLocationCityData;
import com.loongtech.bi.manager.player.VersionAndHardwarePlayerManager.UserLoginLocationProvinceData;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 玩家分析-留存分析Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("session")
@RequestMapping("/player/versionAndHardwarePlayer")
public class VersionAndHardwarePlayerBiAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private List<UserLoginLocationCityData> dataList1;  // 前端要显示的数据1
	private ChartUIData chartData1;  // 前端要显示的柱状图1数据
	
	private List<UserLoginLocationProvinceData> dataList2;  // 前端要显示的数据1
	private ChartUIData chartData2;  // 前端要显示的柱状图1数据
	
	@Autowired
	private VersionAndHardwarePlayerManager versionAndHardwarePlayerManager;
	
    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dstart, @RequestParam String dend, @RequestParam String platform, @RequestParam String channel, @RequestParam String server, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            dateStart = Time.formatDate(dstart, Time.TIMEPATTERN_SIMPLE);
            dateEnd = Time.formatDate(dend, Time.TIMEPATTERN_SIMPLE);
            queryPlatform = platform;
            queryChannel = channel;
            queryServer = server;

            dataList1 = versionAndHardwarePlayerManager.getUserLoginLocationCityData(dateStart, dateEnd, platform, channel, server);
            if (dataList1 != null && dataList1.size() > 0) {
                chartData1 = new ChartUIData();
                chartData1.setJsonType(ChartType.COLUMN.type);
                chartData1.setJsonStr(new ChartTool(dataList1, UserLoginLocationCityData.class, "").loadData(chartData1.getJsonType()));
            } else {
                chartData1 = null;
            }

            dataList2 = versionAndHardwarePlayerManager.getUserLoginLocationProvinceData(dateStart, dateEnd, platform, channel, server);
            if (dataList2 != null && dataList2.size() > 0) {
                chartData2 = new ChartUIData();
                chartData2.setJsonType(ChartType.COLUMN.type);
                chartData2.setJsonStr(new ChartTool(dataList2, UserLoginLocationProvinceData.class, "").loadData(chartData2.getJsonType()));
            } else {
                chartData2 = null;
            }

            result.put("res", "0");
            result.put("chartData1", chartData1);
            result.put("chartData2", chartData2);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
	}

    @RequestMapping(value = "updateServer.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateServer(HttpServletRequest request, HttpServletResponse response, String server, String callback) {
        try {
            queryServer = server;
            selectedServer();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("channelItmes", channelItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updateChannel.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updateChannel(HttpServletRequest request, HttpServletResponse response, String channel, String callback) {
        try {
            queryChannel = channel;
            updateServerItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("platformItmes", platformItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @RequestMapping(value = "updatePlatform.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void updatePlatform(HttpServletRequest request, HttpServletResponse response, String platform, String callback) {
        try {
            queryPlatform = platform;
            updateChannelItmes();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "0");
            result.put("serverItmes", serverItmes);
            result.put("channelItmes", channelItmes);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
            return;
        }
    }

    @Override
    public void search() {
    }

    @Override
	protected String csvContent() {
		return "用户地域分布";
	}

	@Override
    protected String csvFileName() {
	    return "用户地域分布";
    }

	public List<UserLoginLocationCityData> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<UserLoginLocationCityData> dataList1) {
		this.dataList1 = dataList1;
	}

	public ChartUIData getChartData1() {
		return chartData1;
	}

	public void setChartData1(ChartUIData chartData1) {
		this.chartData1 = chartData1;
	}

	public List<UserLoginLocationProvinceData> getDataList2() {
		return dataList2;
	}

	public void setDataList2(List<UserLoginLocationProvinceData> dataList2) {
		this.dataList2 = dataList2;
	}

	public ChartUIData getChartData2() {
		return chartData2;
	}

	public void setChartData2(ChartUIData chartData2) {
		this.chartData2 = chartData2;
	}
}
